<template>
  <basic-container>
    <el-form ref="queryForm"
             :model="queryParams"
             :inline="true"
             label-width="80px">
      <el-form-item label="配置名称"
                    prop="searchParams.search_eq_paramName">
        <el-input v-model="queryParams.searchParams.search_eq_paramName"
                  placeholder="请输入配置名称"/>
      </el-form-item>
      <el-form-item label="配置值"
                    prop="searchParams.search_eq_paramVal">
        <el-input v-model="queryParams.searchParams.search_eq_paramVal"
                  placeholder="请输入配置值"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   icon="el-icon-search"
                   size="mini"
                   @click="handleQuery">搜索
        </el-button>
        <el-button icon="el-icon-refresh"
                   size="mini"
                   @click="resetQuery">重置
        </el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10"
            class="mb8">
      <el-col :span="1.5">
        <el-button type="primary"
                   icon="el-icon-plus"
                   size="mini"
                   @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
                type="success"
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
        >编辑
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
        >删除
        </el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading"
              :data="userList"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="配置名称" align="center" prop="paramName"/>
      <el-table-column label="类型" align="center" prop="type"  :formatter="typeFormate"/>
      <el-table-column label="配置值" align="center" prop="paramVal"/>
      <el-table-column label="说明" align="center" prop="remark">
      </el-table-column>
      <el-table-column label="操作"
                       align="center"
                       class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="small"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
          >编辑
          </el-button>
          <el-button size="small"
                     type="text"
                     icon="el-icon-delete"
                     class="deleteBtn"
                     @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"/>


    <!-- 添加或编辑收费员信息对话框 -->
    <el-dialog :title="title[statusDialog]"
               :visible.sync="open"
               width="40%"
               append-to-body
               :close-on-click-modal="false">
      <el-form ref="form"
               :model="form"
               :rules="rules"
               label-width="80px">
        <el-form-item label="配置名称"
                      prop="paramName">
          <el-input v-model="form.paramName"
                    placeholder="请输入配置名称"/>
        </el-form-item>
        <el-form-item label="配置项" prop="type">
          <el-radio-group v-model="form.type" @change="changeRadio" :disabled="statusDialog=='edit'">
            <el-radio v-for="dict in typeOptions"
                      :key="dict.dictValue"
                      :label="dict.dictValue">{{dict.dictLabel}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="配置值"
                      prop="paramVal">
          <el-input v-model="form.paramVal"
                    placeholder="请输入配置值" style="width:initial" v-if="form.type=='1'"/>
          <el-color-picker size="mini"
                           style="padding-top:18px;"
                           class="theme-picker"
                           popper-class="theme-picker-dropdown"
                           v-model="colorVal" v-if="form.type=='1'"></el-color-picker>

          <el-select v-model="form.paramVal" placeholder="请选择" v-if="form.type=='2'" @change="selectThem">
            <el-option
                    v-for="dict in themOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="说明"
                      prop="remark">
          <el-input type="textarea" v-model="form.remark"
                    placeholder="请输入说明"/>
        </el-form-item>

      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button type="primary"
                   @click="submitForm">确 定
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </basic-container>
</template>

<script>
  import {addConf, delConf, listConf, updateConf} from "@/api/system/conf";
  import color from "@/mixins/color";
  import {setTheme} from "@/util/util";

  export default {
    mixins: [color()],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 用户信息表格数据
        userList: [],
        // 弹出层标题
        title: {
          add: '新增字典',
          edit: '编辑字典',
        },
        statusDialog: "",
        // 是否显示弹出层
        open: false,
        typeOptions:[],
        themOptions:[],

        colorVal:'#409EFF',
        themVal:'default',
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          searchParams: {
            search_eq_paramName: undefined,
            search_eq_paramVal: undefined,
            search_eq_status: undefined,
          }
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          paramName: [
            {required: true, message: "配置名称不能为空", trigger: "blur"}
          ],
          paramVal: [
            {required: true, message: "配置值不能为空", trigger: "blur"}
          ],
        }
      }

    },
    watch: {
      colorVal(val, oldVal) {
        this.form.paramVal=val;
        this.$store.commit("SET_COLOR_NAME", val);
        this.updateTheme(val, oldVal);
      }
    },
    created() {
      this.getList();
      this.getDicts("sys_config_type").then(response=>{
        this.typeOptions =response.data.data;
      });
      this.getDicts("sys_them").then(response=>{
        this.themOptions =response.data.data;
      });
    },
    methods: {
      /** 搜索按钮操作 */
      handleQuery() {
        for (let key in this.queryParams.searchParams) {
          if(this.queryParams.searchParams[key]!=undefined){
            if (this.queryParams.searchParams[key] == "") {
              this.queryParams.searchParams[key] = undefined;
              continue;
            }
            if(this.queryParams.searchParams[key].length>0){
              if(!this.queryParams.searchParams[key].constructor == Array){
                if(!(this.queryParams.searchParams[key].replace(" ", "").trim().length > 0)){
                  this.queryParams.searchParams[key] = undefined;
                  continue;
                }
              }
            }
          }
        }
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          paramName: undefined,
          paramVal: this.themVal,
          remark: undefined,
          type: "2",
        };
        this.resetForm("form");
      },
      /** 查询收费员信息列表 */
      getList() {
        this.loading = true;
        listConf(this.queryParams).then(response => {
          const data = response.data.data;
          this.userList = data.rows;
          this.total = data.total;
          this.loading = false;
        });
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        // this.ids = selection.map(item => item.id)
        this.ids = selection;
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },

      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.statusDialog = 'add';
      },
      /** 编辑按钮操作 */
      handleUpdate(row) {
        if (row.id) {
          this.form = JSON.parse(JSON.stringify(row));
        } else {
          this.form = JSON.parse(JSON.stringify(this.ids[0]));
        }
        if(this.form.type=='1'){
          this.colorVal=this.form.paramVal;
        }
        this.open = true;
        this.statusDialog = 'edit';
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateConf(this.form).then(response => {
                const data = response.data;
                if (response.status === 200 && data.code === "00") {
                  this.msgSuccess("编辑成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addConf(this.form).then(response => {
                const data = response.data;
                if (response.status === 200 && data.code === "00") {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const userIds = row.id || this.ids;
        let tt = '';
        if (row.id == undefined) {
          tt = '是否确认删除【' + this.ids.length + '】条数据吗?';
        } else {
          tt = '是否确认删除为"' + row.dictName + '"的数据项?';
        }
        this.$confirm(tt, "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delConf(userIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      changeRadio(value){
        this.form.paramVal=undefined;
        if(value==='1'){//换色
          this.form.paramVal=this.colorVal;
        }else if(value==='2'){//主题
          this.form.paramVal=this.themVal;
        }
      },
      selectThem(val){
        this.$store.commit("SET_THEME_NAME", val);
        setTheme(val);
      },
      typeFormate(row){
        return this.selectDictLabel(this.typeOptions,row.type);
      }

    }
  };
</script>

<style>

  .theme-picker .el-color-picker__trigger {
    vertical-align: middle;
  }

  .theme-picker-dropdown .el-color-dropdown__link-btn {
    display: none;
  }

</style>
